<?php
/**
 * WQPHP封装工具类，仅供学习研究使用，禁止任何形式的转载发布
 * User: Jessica@aweb.wang
 * Date: 2023/3/10 14:50
 * Email: <1194323013@qq.com>
 *                            _ooOoo_
 *                           o8888888o
 *                           88" . "88
 *                           (| -_- |)
 *                           O\  =  /O
 *                        ____/`---'\____
 *                      .'  \\|     |//  `.
 *                     /  \\|||  :  |||//  \
 *                    /  _||||| -:- |||||-  \
 *                    |   | \\\  -  /// |   |
 *                    | \_|  ''\---/''  |   |
 *                    \  .-\__  `-`  ___/-. /
 *                  ___`. .'  /--.--\  `. . __
 *               ."" '<  `.___\_<|>_/___.'  >'"".
 *              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *              \  \ `-.   \_ __\ /__ _/   .-` /  /
 *         ======`-.____`-.___\_____/___.-`____.-'======
 *                            `=---='
 *        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *                      Buddha Bless, No Bug !
 **/

namespace app\common\utils;


use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Exception\TeaError;
use AlibabaCloud\Tea\Model;
use AlibabaCloud\Tea\Utils\Utils;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use Darabonba\OpenApi\Models\Config;

class SmsUtils
{
//    public static function sendVefiry($phoneNum){
//        $code=rand(100000,999999);
//        $content="您的验证码为：".$code."为了您的安全，请不要将验证码分享给其他人";
//        self::send($phoneNum,$content);
//    }

    public static function send($phoneNum){
        $code=rand(100000,999999);

        $client=self::createClient(\think\facade\Config::get('sms.ak'),\think\facade\Config::get('sms.ks'));
        $sendSmsRequest=new SendSmsRequest([
            'signName'=>\think\facade\Config::get('sms.signName'),
            'templateCode'=>\think\facade\Config::get('sms.codeTemplateId'),
            'phoneNumbers'=>"$phoneNum",
            "templateParam"=>"{\"code\":\"$code\"}"
        ]);
        $runtime=new RuntimeOptions([]);
        try{
            $client->sendSmsWithOptions($sendSmsRequest, $runtime);
        }catch (\Exception $error){
            if (!($error instanceof TeaError)) {
                $error = new TeaError([], $error->getMessage(), $error->getCode(), $error);
            }
            throw $error;
            // 如有需要，请打印 error
            Utils::assertAsString($error->message);
        }
        return $code;
    }
    public static function createClient($accessKeyId,$accessKeySecret){
        $config=new Config([
            "accessKeyId"=>$accessKeyId,
            "accessKeySecret"=>$accessKeySecret
        ]);
        $config->endpoint="dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }
}